package com.rd.student.jike.class2.lihongming.week724_730.Day727;

import java.io.*;

/**
 * @Auther:Heinrich
 * @Date:2023/7/27-07-27-14:32
 * @Description:c230727
 * @version:1.0
 */
public class TestStream {
    public static void main(String[] args) throws Exception{
        copyFile("stream.txt","streamCopy.txt");
    }

    public  static  void testRead() throws Exception{
        InputStream is = new FileInputStream("stream.txt");
        while (true){
            int len = is.read();
            if(len == -1)break;
            byte[] arr = new byte[1];
            arr[0] = (byte)len;
            System.out.print(new String(arr));
        }
        is.close();
    }
    public  static  void testRead1() throws Exception{
        File file = new File("stream.txt");
        InputStream is = new FileInputStream("stream.txt");
        int con = 0;
        while (true){
            byte[] arr = new byte[6];
            int len = is.read(arr);
            if(len == -1)break;
            String concat = new String(arr,0,len);
            System.out.println(len + "---" + concat);
            con++;
        }
        System.out.println();
        System.out.println(con);
        is.close();
    }
    public static void testAutoClose(){
        File file = new File("stream.txt");
        try (InputStream is =new FileInputStream(file);){
            int con = 0;
            while (true){
                byte[] arr = new byte[6];
                int len = is.read(arr);
                if(len == -1)break;
                String concat = new String(arr,0,len);
                System.out.println(len + "---" + concat);
                con++;
            }
            System.out.println();
            System.out.println(con);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public  static  void testRead2() throws Exception{
        File file = new File("stream.txt");
        InputStream is = new FileInputStream("stream.txt");
        int con = 0;
        while (true){
            byte[] arr = new byte[6];
            int len = is.read(arr,0,arr.length);
            if(len == -1)break;
            String concat = new String(arr,0,len);
            System.out.println(len + "---" + concat);
            con++;
        }
        System.out.println();
        System.out.println(con);
        is.close();
    }

    public static void copyFile(String srcPath,String descPAth)throws Exception {
              InputStream is = new FileInputStream(srcPath);
              OutputStream os = new FileOutputStream(descPAth);
              byte[] arr = new byte[1024];
              int len = 0;
              while ((len=is.read(arr)) != -1){
                  os.write(arr,0,len);
              }
              os.close();
              is.close();
    }

    public static void testWrite(String str) {


    }
}
